package com.example.dwhne.leonweather;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import java.util.Objects;

import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.basic.Basic;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.NowBase;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;

public class CityManage extends AppCompatActivity {
    private List<Map<String,Object>> weatherList;
    private String cityCNs[]=new String[5];
    private int cityNum=0;
    private int counti=0;
    private ListView listView;
    private SimpleAdapter sim_Adapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manage);
        dataGetIni();
        listView=findViewById(R.id.weatherManage_ListView);
        weatherList = new ArrayList<Map<String,Object>>();
        for(int i=0;i<cityNum;i++){
            weatherList.add(i,null);
        }
        sim_Adapter2=new SimpleAdapter(CityManage.this,weatherList,R.layout.citylist, new String[]{"Location","Icon","Tmp","Cond_txt"}, new int[]{R.id.weathetrm_City,R.id.weathetrm_Icon,R.id.weathetrm_Temp,R.id.weathetrm_Weather});
        listView.setAdapter(sim_Adapter2);
        for(int j=0;j<cityNum;j++){
            final int finalJ = j;

            HeWeather.getWeatherNow(CityManage.this,cityCNs[j], Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultWeatherNowBeanListener() {
                @Override
                public void onError(Throwable throwable) {
//                    Log.i("Test", "获取失败");
                }

                @Override
                public void onSuccess(List<Now> list) {
//                    Log.i("Test", "获取成功");
                    Now now=list.get(0);
                    Basic basic=now.getBasic();
                    NowBase nowBase=now.getNow();
//                        Update uptime=now.getUpdate();
                    //List lll=new ArrayList();
                    //lll.add(basic.getLocation());
                    Map<String,Object> map=new HashMap<String, Object>();
                    map.put("Location",basic.getLocation());//城市名称
                    int pngID=getResources().getIdentifier("a"+nowBase.getCond_code(), "drawable", getApplicationInfo().packageName);
                    map.put("Icon",pngID);//城市天气状况代码
                    map.put("Tmp",nowBase.getTmp()+"℃");//温度
                    map.put("Cond_txt",nowBase.getCond_txt());
//                    map.put("Wind_dir",nowBase.getWind_dir());
//                    map.put("Wind_sc",nowBase.getWind_sc());
                    weatherList.set(finalJ,map);
                    sim_Adapter2.notifyDataSetChanged();
//                    counti++;
                }
            });

        }
//        counti=0;


        //列表点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Log.i("Test", "position"+position);
                SharedPreferences.Editor hPosition = getSharedPreferences("historyPosition", MODE_PRIVATE).edit();
                hPosition.putInt("Position", position);
                hPosition.commit();
                Intent intent=new Intent(CityManage.this,WeatherShow.class);//构建一个intent对象，第一个参数：上下文，第二个：目标活动
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);//执行intent活动，跳转至第二个页面
            }
        });
        //按钮点击事件
        FloatingActionButton cityMangementFab=findViewById(R.id.cityManage_fab);
        cityMangementFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(CityManage.this,CitySelect.class);
                startActivity(intent);
            }
        });
    }
    private void dataGetIni(){
        SharedPreferences c=getSharedPreferences("cities",MODE_PRIVATE);
        for(int i=0;i<5;i++){
            cityCNs[i]=c.getString("CN"+i,null);
            if(cityCNs[i]!=null){
                cityNum++;
            }
        }

    }
}
